<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class CreditRecord extends \app\BaseModel
{
    protected $name = "massage_coach_credit_record";
    public function getValueAttr($value, $data)
    {
        return !empty($value) ? floatval($value) : $value;
    }
    public function getOrderPriceAttr($value, $data)
    {
        return !empty($value) ? floatval($value) : $value;
    }
    public function dataAdd($data)
    {
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("status desc,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function getCoachValue($uniacid, $alh, $credit_config, $city_id = 0)
    {
        $arr = ["1" => "order_empty", "2" => "add_order_empty", "3" => "time_long_empty", "4" => "repeat_order_empty", "5" => "good_evaluate_empty", "6" => "refund_order_empty", "7" => "refuse_order_empty", "8" => "bad_evaluate_empty"];
        foreach ($arr as $k => $v) {
            if($credit_config[$v] == 0) {
                $type_arr[] = $k;
            }
        }
        $config_model = new Config();
        $config = $config_model->dataInfo(["uniacid" => $uniacid]);
        $level_cycle = $config["level_cycle"];
        $type = $config["is_current"];
        if($level_cycle == 1) {
            if($type == 1) {
                $start_time = strtotime("this week Monday");
                $end_time = strtotime("this week Sunday") + 86400 - 1;
            } else {
                $start_time = strtotime("last week Monday");
                $end_time = strtotime("last week Sunday") + 86400 - 1;
            }
        } elseif($level_cycle == 2) {
            if($type == 1) {
                $start_time = mktime(0, 0, 0, date("m"), 1, date("Y"));
                $end_time = mktime(23, 59, 59, date("m"), date("t"), date("Y"));
            } else {
                $start_time = mktime(0, 0, 0, date("m") - 1, 1, date("Y"));
                $end_time = mktime(23, 59, 59, date("m") - 1, date("t", $start_time), date("Y"));
            }
        } elseif($level_cycle == 3) {
            $quarter = $type == 1 ? ceil(date("n") / 3) : ceil(date("n") / 3) - 1;
            $start_time = mktime(0, 0, 0, $quarter * 3 - 2, 1, date("Y"));
            $end_time = mktime(0, 0, 0, $quarter * 3 + 1, 1, date("Y"));
        } elseif($level_cycle == 4) {
            if($type == 1) {
                $start_time = mktime(0, 0, 0, 1, 1, date("Y"));
                $end_time = mktime(23, 59, 59, 12, 31, date("Y"));
            } else {
                $year = date("Y") - 1;
                $start_time = mktime(0, 0, 0, 1, 1, $year);
                $end_time = mktime(23, 59, 59, 12, 31, $year);
            }
        } elseif($level_cycle == 5) {
            $day = date("d", time());
            if($type == 1) {
                if(15 < $day) {
                    $start_time = strtotime(date("Y-m-16", time()));
                    $end_time = strtotime(date("Y-m-t", time())) + 86399;
                } else {
                    $start_time = strtotime(date("Y-m-01", time()));
                    $end_time = strtotime(date("Y-m-16", time())) - 1;
                }
            } elseif(15 < $day) {
                $start_time = strtotime(date("Y-m-01", time()));
                $end_time = strtotime(date("Y-m-16", time())) - 1;
            } else {
                $start_time = strtotime(date("Y-m-16", strtotime("-1 month")));
                $end_time = strtotime(date("Y-m-t", strtotime("-1 month"))) + 86399;
            }
        }
        $dis = ["a.status" => 2, "a.auth_status" => 2, "a.uniacid" => $uniacid];
        if(!empty($city_id)) {
            $dis["a.city_id"] = $city_id;
        }
        $where = [];
        if(!empty($start_time)) {
            $where[] = ["b.create_time", "between", $start_time . "," . $end_time];
            $where[] = ["b.type", "in", $type_arr];
        }
        $data = \think\facade\Db::name("massage_service_coach_list")->alias("a")->join("massage_coach_credit_record b", "a.id = b.coach_id")->where($dis)->where(function ($query) use ($where) {
            $query->whereOr($where);
        })->field(["b.*,sum(b.value) as total_value,a.sh_time", $alh])->group("a.id")->select()->toArray();
        return $data;
    }
    public function updateCoachValue($uniacid, $alh, $credit_config, $city_id = 0)
    {
        $keys = "updateCoachValue_key_updateCoachValue";
        if(getCache($keys, $uniacid)) {
        }
        setCache($keys, 1, 3, $uniacid);
        $dis = ["a.status" => 2, "a.auth_status" => 2, "a.uniacid" => $uniacid];
        if(!empty($city_id)) {
            $dis["a.city_id"] = $city_id;
        }
        $coach_model = new Coach();
        $list = $coach_model->alias("a")->where($dis)->field(["a.id as coach_id,a.coach_name", $alh])->select()->toArray();
        if(!empty($list) && !empty($credit_config["distance"])) {
            foreach ($list as $key => $value) {
                $credit_top = 999;
                foreach ($credit_config["distance"] as $k => $v) {
                    if($value["distance_data"] < $v * 1000) {
                        $credit_top = $k;
                        $inserts[$key] = ["id" => $value["coach_id"], "credit_top" => $credit_top, "coach_name" => $value["coach_name"], "credit_value" => 0];
                    }
                }
            }
            $coach_model->saveAll($inserts);
        }
        $list = $this->getCoachValue($uniacid, $alh, $credit_config, $city_id);
        if(!empty($list)) {
            foreach ($list as $key => $value) {
                $insert[$key] = ["id" => $value["coach_id"], "credit_value" => $value["total_value"]];
            }
            $coach_model->saveAll($insert);
        }
        return true;
    }
    public function getSingleCoachValue($uniacid, $coach_id)
    {
        $config_model = new CreditConfig();
        $credit_config = $config_model->dataInfo(["uniacid" => $uniacid]);
        if($credit_config["status"] == 0) {
            return 0;
        }
        $arr = ["1" => "order_empty", "2" => "add_order_empty", "3" => "time_long_empty", "4" => "repeat_order_empty", "5" => "good_evaluate_empty", "6" => "refund_order_empty", "7" => "refuse_order_empty", "8" => "bad_evaluate_empty"];
        foreach ($arr as $k => $v) {
            if($credit_config[$v] == 0) {
                $type_arr[] = $k;
            }
        }
        $config_model = new Config();
        $config = $config_model->dataInfo(["uniacid" => $uniacid]);
        $level_cycle = $config["level_cycle"];
        $type = $config["is_current"];
        if($level_cycle == 1) {
            if($type == 1) {
                $start_time = strtotime("this week Monday");
                $end_time = strtotime("this week Sunday") + 86400 - 1;
            } else {
                $start_time = strtotime("last week Monday");
                $end_time = strtotime("last week Sunday") + 86400 - 1;
            }
        } elseif($level_cycle == 2) {
            if($type == 1) {
                $start_time = mktime(0, 0, 0, date("m"), 1, date("Y"));
                $end_time = mktime(23, 59, 59, date("m"), date("t"), date("Y"));
            } else {
                $start_time = mktime(0, 0, 0, date("m") - 1, 1, date("Y"));
                $end_time = mktime(23, 59, 59, date("m") - 1, date("t", $start_time), date("Y"));
            }
        } elseif($level_cycle == 3) {
            $quarter = $type == 1 ? ceil(date("n") / 3) : ceil(date("n") / 3) - 1;
            $start_time = mktime(0, 0, 0, $quarter * 3 - 2, 1, date("Y"));
            $end_time = mktime(0, 0, 0, $quarter * 3 + 1, 1, date("Y"));
        } elseif($level_cycle == 4) {
            if($type == 1) {
                $start_time = mktime(0, 0, 0, 1, 1, date("Y"));
                $end_time = mktime(23, 59, 59, 12, 31, date("Y"));
            } else {
                $year = date("Y") - 1;
                $start_time = mktime(0, 0, 0, 1, 1, $year);
                $end_time = mktime(23, 59, 59, 12, 31, $year);
            }
        } elseif($level_cycle == 5) {
            $day = date("d", time());
            if($type == 1) {
                if(15 < $day) {
                    $start_time = strtotime(date("Y-m-16", time()));
                    $end_time = strtotime(date("Y-m-t", time())) + 86399;
                } else {
                    $start_time = strtotime(date("Y-m-01", time()));
                    $end_time = strtotime(date("Y-m-16", time())) - 1;
                }
            } elseif(15 < $day) {
                $start_time = strtotime(date("Y-m-01", time()));
                $end_time = strtotime(date("Y-m-16", time())) - 1;
            } else {
                $start_time = strtotime(date("Y-m-16", strtotime("-1 month")));
                $end_time = strtotime(date("Y-m-t", strtotime("-1 month"))) + 86399;
            }
        }
        $dis = ["a.id" => $coach_id];
        $where = [];
        if(!empty($start_time)) {
            $where[] = ["b.create_time", "between", $start_time . "," . $end_time];
            $where[] = ["b.type", "in", $type_arr];
        }
        $coach_model = new Coach();
        $data = $coach_model->alias("a")->join("massage_coach_credit_record b", "a.id = b.coach_id")->where($dis)->where(function ($query) use ($where){
            $query->whereOr($where);
        })->field("b.id")->group("a.id")->sum("b.value");
        $init_value = $credit_config["init_value"];
        $new_protect_value = $credit_config["new_protect_value"];
        $new_protect_day = time() - $credit_config["new_protect_day"] * 86400;
        $coach_model = new Coach();
        $sh_time = $coach_model->where(["id" => $coach_id])->value("sh_time");
        $init_value = $new_protect_day < $sh_time ? $new_protect_value : $init_value;
        return round($data + $init_value, 4);
    }
}

?>